<?php
namespace App\Common;
class Common
{

	private static $_ch;
	static function CheckMobile($mobile)
	{
		$error_code = 0;
		if(empty($mobile)
				|| !preg_match("/^1[3-8]\d{9}$/i", $mobile, $matches)) {
			$error_code = ErrorDefine::INVALID_MOBILE;
		}
		return $error_code;
	}

	static function CheckPassword($password)
	{
		$error_code = 0;
		if(strlen($password) < 6) {
			$error_code = ErrorDefine::PASSWORD_TOO_SHORT;
		} else if(strlen($password) > 12) {
			$error_code = ErrorDefine::PASSWORD_TOO_LONG;
		} else if(!preg_match("/^[0-9a-z]+$/i", $password, $matches)) {
			$error_code = ErrorDefine::ILLEGAL_PASSWORD;
		}
		return $error_code;
	}

	static function CheckVerifyCode($verify_code)
	{
		$error_code = 0;
		if(empty($verify_code) 
				|| !is_numeric($verify_code) 
				|| !is_int($verify_code+0)) {
			$error_code = ErrorDefine::ILLEGAL_VERIFY_CODE;
		}
		return $error_code;
	}


	/*
	 * 以下方法用于访问第三方接口
	 */
	private static function _close() {
		if (is_resource(self::$_ch)) {
			curl_close(self::$_ch);
		}
	
		return true;
	}
	private static function _httpGet($url, $query=array()) {
	
		if (!empty($query)) {
			$url .= (strpos($url, '?') === false) ? '?' : '&';
			$url .= is_array($query) ? http_build_query($query) : $query;
		}
	
		curl_setopt(self::$_ch, CURLOPT_URL, $url);
		curl_setopt(self::$_ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt(self::$_ch, CURLOPT_HEADER, 0);
		curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt(self::$_ch, CURLOPT_SSLVERSION, 1);
	
		$ret= self::_execute();
		self::_close();
		//        var_dump($ret);exit();
		return $ret;
	
	}
	
	private static function _httpPost($url, $query=array(), $is_urlcode=true) {
		if (is_array($query)) {
			foreach ($query as $key => $val) {
				if($is_urlcode){
					$encode_key = urlencode($key);
				}else{
					$encode_key = $key;
				}
				if ($encode_key != $key) {
					unset($query[$key]);
				}
				if($is_urlcode){
					$query[$encode_key] = urlencode($val);
				}else{
					$query[$encode_key] = $val;
				}
	
			}
		}
		$header[] = "Content-type: application/json";
		curl_setopt(self::$_ch, CURLOPT_HTTPHEADER, $header);
		curl_setopt(self::$_ch, CURLOPT_URL, $url);
		curl_setopt(self::$_ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt(self::$_ch, CURLOPT_HEADER, 0);
		curl_setopt(self::$_ch, CURLOPT_POST, true );
		curl_setopt(self::$_ch, CURLOPT_POSTFIELDS, $query);
		curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt(self::$_ch, CURLOPT_SSLVERSION, 1);
	
	
		$ret = self::_execute();
		self::_close();
		return $ret;
	}
	


    private static function _put($url, $query = array()) {
        curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'PUT');

        return self::_httpPost($url, $query);
    }

    private static function _delete($url, $query = array()) {
        curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'DELETE');

        return self::_httpPost($url, $query);
    }

    private static function _head($url, $query = array()) {
        curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'HEAD');

        return self::_httpPost($url, $query);
    }

    private static function _execute() {
        $response = curl_exec(self::$_ch);
        $errno = curl_errno(self::$_ch);

        if ($errno > 0) {
            throw new \Exception(curl_error(self::$_ch), $errno);
        }
        return  $response;
    }
	
	public static function callWebServer($queryUrl, $param='', $method='get', $is_json=true, $is_urlcode=true) {
		if (empty($queryUrl)) {
			return false;
		}
	
		$method = strtolower($method);
		$ret = '';
		$param = empty($param) ? array() : $param;
		self::_init();
		if ($method == 'get') {
			$ret = self::_httpGet($queryUrl, $param);
		} elseif($method == 'post') {
			$ret = self::_httpPost($queryUrl, $param, $is_urlcode);
		}
	
		if(!empty($ret)){
			if($is_json){
				return json_decode($ret, true);
			}else{
				return $ret;
			}
		}
		return true;
	}
	
	protected static function _init() {
		self::$_ch = curl_init();
	
		curl_setopt(self::$_ch, CURLOPT_HEADER, true);
		curl_setopt(self::$_ch, CURLOPT_RETURNTRANSFER, true);
		//curl_setopt(self::$_ch, CURLOPT_FRESH_CONNECT, true);
	}
}

?>
